package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.DishDto;
import com.itheima.mapper.DishMapper;
import com.itheima.pojo.Category;
import com.itheima.pojo.Dish;
import com.itheima.pojo.DishFlavor;
import com.itheima.service.CategoryService;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;
     /**
     * 菜品分类查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<DishDto> findPage(Integer page, Integer pageSize, String name) {

        //创建条件对象
        QueryWrapper<Dish> wrapper =new QueryWrapper<>();
        //StringUtils.isNotBlank判断参数是否为空
        wrapper.like(StringUtils.isNotBlank(name),"name",name);
        //时间排序条件
        wrapper.orderByDesc("update_time");

        Page<Dish> pageInfo1 = this.page(new Page<Dish>(page, pageSize), wrapper);

        //创建一个DishDto的分页查询对象
        Page<DishDto> pageInfo2 = new Page<>(page,pageSize);
        //将pageInfo1里面的属性拷贝到pageInfo2里面去

        //1.拷贝除了records这个属性其他以外的所有属性
        BeanUtils.copyProperties(pageInfo1,pageInfo2,"records");
        //2.拷贝records这个集合到dishDtoList中去
        ArrayList<DishDto> dishDtoList =new ArrayList<>();

        log.info(pageInfo1.getRecords().toString());
        for (Dish dish : pageInfo1.getRecords()) {
            //创建DishDto 对象
            DishDto dishDto = new DishDto();
            //把pageinfo1里面的records拷贝到dishDto里面去
             BeanUtils.copyProperties(dish,dishDto);
             //调用categoryService 方法获取Category对象
            Category category = categoryService.getById(dish.getCategoryId());
            dishDto.setCategoryName(category.getName());
            dishDtoList.add(dishDto);
        }
        pageInfo2.setRecords(dishDtoList);

        return pageInfo2;
    }

    /**
     * 新增菜品
     * @param dishDto
     */

    @Override
    @SuppressWarnings("all")
    @Transactional
    public void sveawith(DishDto dishDto) {
        //保存dishdto里面的 dish菜品信息
        this.save(dishDto);
        //保存dishdto里面的 dishFlavor口味信息 注意要获取菜品菜品里面的id值
        List<DishFlavor> flavors = dishDto.getFlavors();
        //往flavors设置id的值
        //               DishFlavor对象        给id属性设置值
        flavors.forEach(flavor -> flavor.setDishId(dishDto.getId()));
        dishFlavorService.saveBatch(flavors);

    }

     /**
     * 根据id查询菜品信息和对应的口味信息
     * @param id
     * @return
     */
    @Override
    public DishDto findWithById(Long id) {
        //查询菜品里面的信息
        Dish dish = this.getById(id);

        DishDto dishDto = new DishDto();
        //查询当前菜品里面的口味信息
        QueryWrapper<DishFlavor> wrapper =new QueryWrapper<>();
        wrapper.eq("dish_id",id);
        List<DishFlavor> list = dishFlavorService.list(wrapper);

        dishDto.setFlavors(list);
        //将菜品和口味对象封装到dto中去
        BeanUtils.copyProperties(dish,dishDto);

        return dishDto;
    }

    /**
     * 修改菜品信息
     * @param dishDto
     */

    @Override
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish里面的信息
        this.updateById(dishDto);

        //清理当前菜品对应口味数据---dish_flavor表的delete操作
        QueryWrapper<DishFlavor> wrapper=new QueryWrapper<>();
        wrapper.eq("dish_id",dishDto.getId());
        dishFlavorService.remove(wrapper);

        //添加当前提交过来的口味数据---dish_flavor表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.forEach(flavor -> flavor.setDishId(dishDto.getId()));
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 批量启停
     * @param status
     * @param ids
     */

    @Override
    public void statusByIds(Integer status, Long[] ids) {
        for (Long id : ids) {
            //获取dish对象
            Dish dish = this.getById(id);
            //设置修改的值
            dish.setStatus(status);

            this.updateById(dish);

        }

    }


}
